
package graphics2d;

import static java.lang.Math.atan;
import static java.lang.Math.cos;
import static java.lang.Math.sin;


public class Motion {
    
    protected final Matrix object;
    protected final int pivot_point;
    protected Matrix conversions;
    
    Motion(Matrix object) {
        this(object, 0);
    }
    Motion(Matrix object, int pivot_point) {
        this.object = object;
        this.pivot_point = pivot_point;
        double[] arr = new double[]{1, 0, 0, 0, 1, 0, 0, 0, 1};
        conversions = new Matrix(3, arr);
    }
    
    public Motion transfer(double a, double b) {
        double[] arr = new double[]{1, 0, a, 0, 1, b, 0, 0, 1};
        Matrix transfer = new Matrix(3, arr);
        conversions = transfer.multiply(conversions);
        
        return this;
    }
    public Motion rotate(double angle) {
        angle *= atan(1)/45;
        double[] arr = new double[]{cos(angle), -sin(angle), 0, sin(angle), cos(angle), 0, 0, 0, 1};
        Matrix rotate = new Matrix(3, arr);
        conversions = rotate.multiply(conversions);
        
        return this;
    }
    public Motion scale(double kx, double ky) {
        double[] arr = new double[]{kx, 0, 0, 0, ky, 0, 0, 0, 1};
        Matrix scale = new Matrix(3, arr);
        conversions = scale.multiply(conversions);
        
        return this;
    }
    public Motion reflectX() {
        double[] arr = new double[]{1, 0, 0, 0, -1, 0, 0, 0, 1};
        Matrix reflect = new Matrix(3, arr);
        conversions = reflect.multiply(conversions);
        
        return this;
    }
    public Motion reflectY() {
        double[] arr = new double[]{-1, 0, 0, 0, 1, 0, 0, 0, 1};
        Matrix reflect = new Matrix(3, arr);
        conversions = reflect.multiply(conversions);
        
        return this;
    }
    public Motion reflectO() {
        double[] arr = new double[]{-1, 0, 0, 0, -1, 0, 0, 0, 1};
        Matrix reflect = new Matrix(3, arr);
        conversions = reflect.multiply(conversions);
        
        return this;
    }
    
    public Motion rotate(double x0, double y0, double angle) {
        return transfer(-x0, -y0).rotate(angle).transfer(x0, y0);
    }
    
    public Motion rotatePP(double angle) {
        return rotate(conversions.multiply(object).cell(0, pivot_point), conversions.multiply(object).cell(1, pivot_point), angle);
    }
}
